package cn.dengta.webapp.im.entity;

import java.util.Date;
import javax.annotation.Nullable;

import cn.dengta.webapp.base.entity.TimeBasedEntity;
import cn.dengta.webapp.trade.service.UseCoinFreePartAware;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.*;
import org.apache.commons.lang3.StringUtils;

/**
 * 红包发送记录表
 *
 * @author Ready
 * @since 2022-07-05
 */
@Setter
@Getter
@Accessors(chain = true)
public class RedPacket extends TimeBasedEntity implements UseCoinFreePartAware {

	private static final long serialVersionUID = 1L;

	/** 群红包过期时间（毫秒值）：目前是 2 小时 */
	public static final long TIMEOUT_IN_MS = 2 * EasyDate.MILLIS_OF_HOUR;
	/** 个人红包过期时间（毫秒值）：目前是 24 小时 */
	public static final long PERSONAGE_TIMEOUT_IN_MS = 24 * EasyDate.MILLIS_OF_HOUR;
	/** 每人在单个红包内领取的钻石额度上限。 */
	public static final int MAX_COIN_EVERYONE = 10000;
	/** 个人红包钻石额度上限。 */
	public static final int MAX_USER_COIN_EVERYONE = 100000;

	/** 进行中（默认） */
	public static final Integer INIT = 0,
	/** 已退余额 */
	REFUNDED = 1,
	/** 已领完 */
	EMPTY = 2;

	/** 发用户的ID */
	Long userId;
	/** 收红包用户的ID */
	Long toUserId;
	/** 所在群ID */
	Long groupId;
	/** 红包内的钻石总额 */
	Integer totalCoin;
	/** 红包剩余未领取的免费钻石部分 */
	Integer totalCoinFreePart;
	/** 红包剩余未领取的钻石余额（ 含 remainCoinFreePart ） */
	Integer remainCoin;
	/** 红包剩余未领取的免费钻石部分 */
	Integer remainCoinFreePart;
	/** 拆分的红包总个数 */
	Integer totalCount;
	/** 剩余的红包个数 */
	Integer remainCount;
	/** 业务状态：0=进行中（默认）；1=已退余额；2=已领完 */
	Integer status;
	/** 备注 */
	String remark;

	public RedPacket init(Long userId, Long toUserId, Long groupId, Integer totalCoin, Integer totalCount, @Nullable String remark, Date now) {
		this.userId = userId;
		this.toUserId = toUserId;
		this.groupId = groupId;
		this.totalCoin = remainCoin = totalCoin;
		totalCoinFreePart = remainCoinFreePart = 0;
		this.totalCount = remainCount = totalCount;
		this.remark = StringUtil.toString(remark);
		this.status = INIT;
		this.initTime(now);
		return this;
	}

	public String tokenSource() {
		return id + "-" + groupId + "-" + totalCoin + "-" + addTime.getTime();
	}

	public String token() {
		return id + "|" + Encrypter.md5(tokenSource());
	}

	public static Long parseId(String token) {
		final String idStr = StringUtils.substringBefore(token, "|");
		return Long.parseLong(idStr);
	}

	/**
	 * 领取一次红包
	 */
	public void receiveOnce(int coin, int freePart) {
		remainCoin -= coin;
		remainCoinFreePart -= freePart;
		remainCount--;
	}

	public static final String USER_ID = "user_id";
	public static final String TO_USER_ID = "to_user_id";
	public static final String GROUP_ID = "group_id";
	public static final String TOTAL_COIN = "total_coin";
	public static final String TOTAL_COUNT = "total_count";
	public static final String REMAIN_COUNT = "remain_count";
	public static final String STATUS = "status";
	public static final String REMARK = "remark";

	@Override
	public void useFreePart(Integer freePart) {
		totalCoinFreePart = remainCoinFreePart = Math.abs(freePart);
	}

}
